<?php
require_once (dirname(__FILE__) .'/config/config.inc.php');
require_once (dirname(__FILE__) .'/models/Session.php');

$id_project  = Session::getInstance()->id_project;
$id_owner    = Session::getInstance()->id_owner;
$token       = Session::getInstance()->token;
$lang        = Session::getInstance()->lang;
$return_link = Session::getInstance()->return_link;
$back_link   = Session::getInstance()->back_link;
$tokenNew    = md5('ZNSDie324dziffe_'. $id_owner .'_'. $id_project);

if ($tokenNew != $token)
{
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
    header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
    
    header("Cache-Control: no-store, no-cache, must-revalidate");
    header("Cache-Control: post-check=0, pre-check=0", false);
    header("Pragma: no-cache");
    
    header("Location: ../");
    exit;
}
/* 
echo '<pre>';
print_r($_SESSION['PrestaThemeEditor']);
echo '</pre>';die();
 */
require_once (dirname(__FILE__) .'/models/PrestathemeEditor.php');
$app  = new PrestathemeEditor($id_project, $id_owner, $token, $lang);
$json = $app->init();

if (isset($_GET['ajax']))
{
	if (isset($_POST['styles']))
	{
		$ok = $app->saveStyle($_POST['styles']);
	}
	if (isset($_POST['design']))
	{
		$ok = $app->saveDesign($_POST['design']);
		var_dump($ok);
	}
	die();
}
?>
<html>
    <head>
		<!-- Meta -->
		<meta name='baseUrl' content='<?php echo _BASE_URI_; ?>' />
		
		<!-- Favicon -->
        <link href="<?php echo _BASE_URI_ .'media/img/'; ?>favicon.ico" rel="shortcut icon" />
		
		<!-- main style -->
		<link rel="stylesheet" type="text/css" href="<?php echo _BASE_URI_ .'media/css/'; ?>style.css" />
		
		<!-- buttons style -->
		<link rel="stylesheet" type="text/css" href="<?php echo _BASE_URI_ .'media/css/'; ?>button.css" />
		
		<!-- colorpicker style -->
		<link rel="stylesheet" type="text/css" href="<?php echo _BASE_URI_ .'media/css/colorpicker/'; ?>colorpicker.css" />
		<link rel="stylesheet" type="text/css" href="<?php echo _BASE_URI_ .'media/css/colorpicker/'; ?>layout.css" media="screen" />
		
		<!-- Page Title -->
        <title>Prestatheme Editor</title>
		
		<!-- JQuery -->
        <script type="text/javascript" src="<?php echo _BASE_URI_ .'media/js/JQuery/'; ?>jquery-1.6.1.js"></script>
		
		<!-- Color picker -->
		<script type="text/javascript" src="<?php echo _BASE_URI_ .'media/js/colorpicker/'; ?>colorpicker.js"></script>
		<script type="text/javascript" src="<?php echo _BASE_URI_ .'media/js/colorpicker/'; ?>eye.js"></script>
		<script type="text/javascript" src="<?php echo _BASE_URI_ .'media/js/colorpicker/'; ?>utils.js"></script>
		<script type="text/javascript" src="<?php echo _BASE_URI_ .'media/js/colorpicker/'; ?>layout.js?ver=1.0.2"></script>
		
		<!-- Custom JS -->
        <script type="text/javascript">
			var stylesJSON = '<?php echo $json['styles']; ?>';
			var designJSON = '<?php echo str_replace("'", "\\'", $json['design']); ?>';
		</script>
        <script type="text/javascript" src="<?php echo _BASE_URI_ .'media/js/'; ?>remote-control.js"></script>
        <script type="text/javascript" src="<?php echo _BASE_URI_ .'media/js/'; ?>prestatheme.js"></script>
        <script type="text/javascript" src="<?php echo _BASE_URI_ .'media/js/'; ?>apply-style.prestatheme.js"></script>
        <script type="text/javascript" src="<?php echo _BASE_URI_ .'media/js/'; ?>live-updates.prestatheme.js"></script>
        <script type="text/javascript" src="<?php echo _BASE_URI_ .'media/js/'; ?>commit.prestatheme.js"></script>
    </head>
    
    <body>
        <div id="notifications"></div>
        <div id="header">
            <h1>
				<span class="back-to-view"><a class="btn btn-green" href="<?php echo $back_link; ?>" title="Back to view the project view">Back to your project</a></span>
				Prestatheme Editor
				<span class="get-theme"><a class="btn" href="<?php echo $return_link; ?>" title="Order your theme by the target product in your cart">Get your theme</a></span>
			</h1>
			
        </div>
        
        <div id="remote_controls">
            <ul id="controls_tab">
                <?php echo $app->displayTabs(); ?>
            </ul>
            <div id="controls_container">
                <?php echo $app->displayEditorTools(); ?>
            </div>
        </div>
        
        <iframe id="previewer" src="<?php /*echo $app->getUrl();*/ ?>" frameborder="0" width="100%" height="90%"></iframe>
        
		<!--
        <div id="footer">
            <p>Your a successfully log into the prestatheme editor.</p>
            <p>But it is not yet available.</p>
        </div>
		-->
    </body>
</html>